package com.gz.moreaseManager.sys.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.gz.moreaseManager.core.modal.AjaxResult;
import com.gz.moreaseManager.core.modal.HttpCode;
import com.gz.moreaseManager.core.modal.PageResult;
import com.gz.moreaseManager.sys.entity.Menu;
import com.gz.moreaseManager.sys.entity.Role;
import com.gz.moreaseManager.sys.service.IMenuService;
import com.gz.moreaseManager.utils.UtilTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 菜单信息表 前端控制器
 * </p>
 *
 * @author wike
 * @since 2019-02-19
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private IMenuService menuService;

    /**
     * 菜单管理界面
     *
     * @return 页面模板
     */
    @GetMapping("/tree")
    public ModelAndView tree() { return new ModelAndView("sys/menuManage/tree");}


    /**
     * 查询所有的菜单数据
     *
     * @return 菜单数据
     */
    @GetMapping("menuData")
    public AjaxResult<List<Menu>> menuData() {
        List<Menu> data = menuService.selectList(null);
        return AjaxResult.success(data);
    }

    @GetMapping("listPage")
    public PageResult<Menu> findPageList(String menuCode, String menuName,String menuTop, Integer pageNum, Integer pageSize){

        //分页
        Page paramsPage = new Page();
        paramsPage.setCurrent(pageNum);
        paramsPage.setSize(pageSize);
        //构建查询
        Wrapper wrapper = new EntityWrapper<Menu>();
        if( UtilTool.isNotBlank( menuCode ))
            wrapper.like("menu_code",menuCode).or().like("parent_code",menuCode);
        if( UtilTool.isNotBlank( menuName ))
            wrapper.like("menu_name",menuName);
        if( UtilTool.isNotBlank( menuTop ))
            wrapper.eq("menu_top",menuTop);
        Page<Role> data = menuService.selectPage(paramsPage,wrapper);
        return new PageResult(0, null, data.getTotal(), data.getRecords());
    }

    /**
     * 查询所有菜单数据
     *
     * @return 所有菜单数据
     */
    @PostMapping("findAllMenu")
    public List<Menu> findAllMenu() {
        return menuService.selectList(null);
    }

    /**
     * 添加菜单信息
     *
     * @param menuInfo 菜单信息
     * @return 添加结果
     */
    @PostMapping("addNewMenu")
    public AjaxResult<Boolean> addNewMenu(Menu menuInfo) {
        if (menuInfo == null || UtilTool.isBlank(menuInfo.getMenuName())
                || UtilTool.isBlank(menuInfo.getMenuCode())
                || UtilTool.isBlank(menuInfo.getMenuType())
                || UtilTool.isBlank(menuInfo.getStatus()))
            return AjaxResult.build(HttpCode.ARGS_ERROR, "请求参数错误!");
        if (UtilTool.isBlank(menuInfo.getMenuLink()) && menuInfo.getMenuType() == 2)
            return AjaxResult.build(HttpCode.ARGS_ERROR, "菜单类型为动作必须填写菜单链接!");

        menuInfo.setCreateTime(new Date());
        boolean success = menuService.insert(menuInfo);
        return success ? AjaxResult.success() : AjaxResult.build(HttpCode.FAILED, "添加菜单信息失败!");
    }

    /**
     * 获取菜单的基本信息
     * @param menuId
     * @return
     */
    @GetMapping("findMenuByMenuId")
    public AjaxResult<Menu> findMenuByMenuId( Integer menuId){
        if( UtilTool.isBlank( menuId ))
            return AjaxResult.build(HttpCode.ARGS_ERROR, "请求参数错误!");
        Menu menu = menuService.selectById(menuId);
        return AjaxResult.success(menu);
    }

    /**
     * 更新菜单信息
     *
     * @param menuInfo 菜单信息
     * @return 更新结果
     */
    @PostMapping("editMenu")
    public AjaxResult<Boolean> editMenu(Menu menuInfo) {
        if (menuInfo == null || menuInfo.getMenuId() == null
                || UtilTool.isBlank(menuInfo.getMenuCode())
                || UtilTool.isBlank(menuInfo.getMenuName())
                || UtilTool.isBlank(menuInfo.getMenuType())
                || menuInfo.getStatus() == null)
            return AjaxResult.build(HttpCode.ARGS_ERROR, "请求参数错误!");
        if (UtilTool.isBlank(menuInfo.getMenuLink()) && menuInfo.getMenuType() == 2)
            return AjaxResult.build(HttpCode.ARGS_ERROR, "菜单类型为动作必须填写菜单链接!");
        boolean success = menuService.updateById(menuInfo);
        return success ? AjaxResult.success() : AjaxResult.build(HttpCode.FAILED, "更新菜单信息失败!");
    }


    /**
     * 刪除菜单
     * @param menuId
     * @return
     */
    @PostMapping("delMenu")
    public AjaxResult<Boolean> delMenuByMenuId(Integer menuId){
        if( UtilTool.isBlank( menuId ))
            return AjaxResult.build(HttpCode.ARGS_ERROR, "请求参数错误!");
        boolean rs = menuService.deleteById( menuId );
        return rs ? AjaxResult.success() : AjaxResult.build(HttpCode.FAILED, "删除菜单信息失败!");
    }

}
